import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import { leftRoutes } from "./left";

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/Login/index.vue"),
    meta: {
      title: "登录",
    },
  },
  {
    path: "/",
    component: () => import("../layouts/MainLayout.vue"),
    children: [
      {
        path: "",
        redirect: "/dataList",
      },
      ...leftRoutes,
    ],
  },
];

const router = createRouter({
  // createWebHistory() ||
  history: createWebHashHistory(),
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("token");
  if (to.path !== "/login" && !token) {
    next("/login");
  } else {
    document.title = to.meta.title;
    next();
  }
});

export default router;
